import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterxc/module/common_module.dart';
import 'package:flutterxc/widget/webview.dart';

class SubNav extends StatelessWidget {
  final List<CommonModel> subNavList;
  final String name;

  SubNav({@required this.subNavList, this.name = 'xiaoming'});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          color: Colors.white,
        ),
        child: Padding(
          padding: EdgeInsets.all(12),
          child: _items(context),
        ));
  }

  _items(BuildContext context) {
    if (subNavList == null) return null;
    List<Widget> list = [];
    int separate = (subNavList.length / 2 + 0.5).toInt();
    subNavList.forEach((module){
        list.add(_item(context, module));
    });
    return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: list.sublist(0,separate),
        ),
        Padding(
          padding: EdgeInsets.only(top: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: list.sublist(separate,list.length),
          ),
        )
      ],
    );
  }

  _item(BuildContext context, module) {
    return Expanded(
      flex: 1,
      child: GestureDetector(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => WebView(
                      url: module.url,
                      statusBarColor: module.statusBarColor,
                      hideAppBar: module.hideAppBar)));
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Image.network(
              module.icon,
              width: 26,
              height: 26,
            ),
            SizedBox(
              height: 6,
            ),
            Text(
              module.title,
              style: TextStyle(fontSize: 12),
            )
          ],
        ),
      ),
    );
  }
}
